#include"SqNode.h"
void SqNodePrint(SN* phead)
{
	SN* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
SN* BuySqNode(SqNodeType e)
{
	SN* newnode = (SN*)malloc(sizeof(SN));
	if (newnode == NULL)
	{
		printf("malloc is fail\n");
		exit (-1);
	}
	newnode->data = e;
	newnode->next = NULL;
	return newnode;

}
void SqNodePushBack(SN** pphead, SqNodeType e)
{
	SN*S=BuySqNode(e);
	if (*pphead == NULL)
	{
		*pphead = S;
	}
	else
	{
		SN* cur = *pphead;
		while (cur->next != NULL)
		{
			cur = cur->next;
		}
		cur->next = S;
		S->data = e;
		S->next = NULL;
	}
}
void SqNodePushFront(SN** pphead, SqNodeType e)
{
	SN* S = BuySqNode(e);
	S->next = *pphead;
	*pphead = S;
}
void SqNodePopFront(SN** pphead)
{
	if (*pphead == NULL)
	{
		return;
	}
	else
	{
		SN* start = (*pphead)->next;
		free(*pphead);
		*pphead = start;
	}
}
void SqNodePopBack(SN** pphead)
{
	SN* prev = NULL;
	SN* cur = *pphead;
	if (*pphead == NULL)
	{
		return;
	}
	else if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
		while (cur->next != NULL)
		{
			prev = cur;
			cur = cur->next;
		}
		free(cur);
		prev->next = NULL;
	}
}
SN* SqNodeFind(SN* phead, SqNodeType e)
{
	SN* cur = phead;
	while (cur)
	{
		if (cur->data == e)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SqNodeInsert(SN** pphead, SN* pos, SqNodeType e)
{
	SN* cur = *pphead;
	if (pos == *pphead)
	{
		SqNodePushFront(pphead, e);
	}
	while (cur->next != pos)
	{
		cur = cur->next;
	}
	SN* S = BuySqNode(e);
	cur->next = S;
	S->next = pos;
}
void SqNodeErase(SN** pphead, SN* pos)
{
	SN* cur = *pphead;
	if (pos == *pphead)
	{
		SqNodePopFront(pphead);
	}
	else
	{
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		free(pos);
	}
}